package com.sms.plateform.web.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.sms.plateform.common.finals.Constants;
import com.sms.plateform.common.util.JSONUtil;
import com.sms.plateform.common.util.Utils;
import com.sms.plateform.kernel.entity.MenuInfo;
import com.sms.plateform.kernel.entity.RoleInfo;
import com.sms.plateform.kernel.entity.UserInfo;
import com.sms.plateform.kernel.entity.UserRole;
import com.sms.plateform.kernel.service.IMenuInfoService;
import com.sms.plateform.kernel.service.IRoleInfoService;

@Controller
public class IndexController extends BaseController {
	@Autowired
	private IMenuInfoService menuInfoService;
	@Autowired
	private IRoleInfoService roleInfoService;
	@SuppressWarnings("unchecked")
	@RequestMapping("/index")
	public ModelAndView indexHandel(HttpServletRequest request,HttpServletResponse response) {
		Map<String, Object> info = new HashMap<String, Object>();
		String menucode=this.getUrlParam(request, "menucode");
		
		//获取角色和顶级菜单
		UserRole userRole = (UserRole) this.getSessionObject(request, Constants.SESSION_ROLE);
		Map<String, Object> topMenu = menuInfoService.getTopMenu(userRole);
		info.put("topMenu", topMenu);
		info.put("currentMenu", 10);
		//右下角用户信息
		UserInfo userInfo = this.getUserInfo(request);
		info.put("userinfo", userInfo.getUsername());
		return new ModelAndView("index", "info", info);
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping("/menuleft")
	public String listMenuInfoHandel(HttpServletRequest request,HttpServletResponse response,ModelMap model) throws IOException {
		try {
			UserRole roleInfo = (UserRole) this.getSessionObject(request, Constants.SESSION_ROLE);
			String menucode=this.getUrlParam(request, "menucode");//2级菜单
			List<MenuInfo> listMenuInfo2=this.menuInfoService.listMenuRoleInfoByRidAndParentCode(Utils.trim(roleInfo.getId()), menucode);
			model.put("listMenuInfo2", listMenuInfo2);
			response.getWriter().write(JSONUtil.map2json(model));
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return null;
	}
}